//
//  YHConstant+Method.m
//  YHConstant
//
//  Created by 98du on 2021/11/22.
//

#import "YHConstant+Method.h"
#import <objc/message.h>

void yh_swizzleMethod(Class theClass, SEL originalSelector, SEL swizzledSelector) {
    Method originalMethod = class_getInstanceMethod(theClass, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(theClass, swizzledSelector);
    
    BOOL didAddMethod = class_addMethod(theClass,
                                        originalSelector,
                                        method_getImplementation(swizzledMethod),
                                        method_getTypeEncoding(swizzledMethod));
    
    if (didAddMethod) {
        class_replaceMethod(theClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    }else{
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}


#pragma mark - animation

CGFloat YHAnimation_Rate(void){
    return 3.0;
};

CGFloat YHAnimation_Linear(CGFloat t){
    return t;
}

CGFloat YHAnimation_EaseIn(CGFloat t){
    return powf(t, YHAnimation_Rate());
}

CGFloat YHAnimation_EaseOut(CGFloat t){
    return 1.0-powf((1.0-t), YHAnimation_Rate());
}

CGFloat YHAnimation_EaseInOut(CGFloat t){
    t *= 2;
    if (t < 1){
        return 0.5f * powf (t, YHAnimation_Rate());
    }
    else{
        return 0.5f * (2.0f - powf(2.0 - t, YHAnimation_Rate()));
    }
}

CGFloat YHAnimation_EaseInBounce(CGFloat t){
    if (t < 4.0 / 11.0) {
        return 1.0 - (powf(11.0 / 4.0, 2) * powf(t, 2)) - t;
    }
    
    if (t < 8.0 / 11.0) {
        return 1.0 - (3.0 / 4.0 + powf(11.0 / 4.0, 2) * powf(t - 6.0 / 11.0, 2)) - t;
    }
    
    if (t < 10.0 / 11.0) {
        return 1.0 - (15.0 /16.0 + powf(11.0 / 4.0, 2) * powf(t - 9.0 / 11.0, 2)) - t;
    }
    
    return 1.0 - (63.0 / 64.0 + powf(11.0 / 4.0, 2) * powf(t - 21.0 / 22.0, 2)) - t;
}

CGFloat YHAnimation_EaseOutBounce(CGFloat t){
    if (t < 4.0 / 11.0) {
        return powf(11.0 / 4.0, 2) * powf(t, 2);
    }
    
    if (t < 8.0 / 11.0) {
        return 3.0 / 4.0 + powf(11.0 / 4.0, 2) * powf(t - 6.0 / 11.0, 2);
    }
    
    if (t < 10.0 / 11.0) {
        return 15.0 /16.0 + powf(11.0 / 4.0, 2) * powf(t - 9.0 / 11.0, 2);
    }
    
    return 63.0 / 64.0 + powf(11.0 / 4.0, 2) * powf(t - 21.0 / 22.0, 2);
}



@implementation YHConstant (Method)

@end
